package servlet.permission;

import bean.dto.PermissionQueryPage;
import bean.pojo.Permissions;
import bean.vo.PermissionsOption;
import bean.vo.RouterVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.PermissionService;
import service.impl.PermissionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(name = "PermissionManagement", value = "/permission/*")
public class PermissionManagement extends BaseServlet {
    /**
     * 根据角色id获取角色权限
     */
    public R findByRoleId(HttpServletRequest request) {
        Integer roleId = NumberFormat.stringToInteger(request.getParameter("roleId"));
        PermissionService permissionService = new PermissionServiceImpl();

        List<RouterVo> list = permissionService.findByRoleId(roleId);
        System.out.println("用户权限“=" + JSON.toJSONString(list));
        R r;
        r = list != null ? R.ok(list) : R.ok("没有权限");
        return r;
    }

    /**
     * 权限分页查询
     * @param request
     * @return
     */
    public R findByPage(HttpServletRequest request) {
        String str_page = request.getParameter("page");
        System.out.println("PermissionManagement:page" + str_page);
        String str_limit = request.getParameter("limit");
        System.out.println("PermissionManagement:limit" + str_limit);
        PermissionQueryPage queryPage = new PermissionQueryPage();
        if (str_page.trim().length() > 0) {
            try {
                queryPage.setPage(Integer.parseInt(str_page));
            } catch (Exception e) {
                queryPage.setPage(1);
            }
        }
        //设置每页行数
        if (str_limit.trim().length() > 0) {
            try {
                queryPage.setLimit(Integer.parseInt(str_limit));
            } catch (Exception e) {
                queryPage.setLimit(10);
            }
        }
        //设置查询条件
        queryPage.setPermissionName(request.getParameter("permissionName"));
        queryPage.setPermissionParentId(NumberFormat.stringToInteger(request.getParameter("permissionParentId")));

        System.out.println("PermissionManagement:findByPage:queryPage:"+queryPage);
        PermissionService permissionService = new PermissionServiceImpl();
        //满足条件的总条数
        Long  total=permissionService.findByTotal(queryPage);
        System.out.println("PermissionManagement:findByPage:total:"+total);
        //当前页码的数据
        List<Permissions> list=null;
        if(total>0) {
            //当前页码的数据
            list = permissionService.findByPage(queryPage);
            System.out.println("PermissionManagement:findByPage:list:"+list.toString());
        }
        //Data?     Long       addData把data HashMap
        R  r= R.ok().addData("total",total).addData("list",list);
        return r;
    }
    /**
     * 查询所有父菜单信息
     * @param request
     * @return
     */
    public R findAllList(HttpServletRequest request) {
        PermissionService permissionService=new PermissionServiceImpl();
        List<PermissionsOption> list= permissionService.findAllList();
        R r;
        r=list!=null?R.ok(list):R.ok("没有父菜单数据");
        return r;
    }

    /**
     * 添加权限信息
     * @param request
     * @return
     */
    public R add(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader bufferedReader=request.getReader();
        String  req_data=bufferedReader.readLine();
        Permissions permissions = JSONObject.parseObject(req_data, Permissions.class);
        PermissionService permissionService=new PermissionServiceImpl();

        boolean update = permissionService.insert(permissions);
        R r=!update? R.error("添加失败"):R.ok();
        return r;
    }
    /**
     * 根据id删除权限信息
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request) {
        String  id=request.getParameter("permissionId");
        System.out.println(id);
        R  r;
        Integer permissionId=null;
        try{
            permissionId=Integer.parseInt(id);
            PermissionService permissionService=new PermissionServiceImpl();
            boolean update = permissionService.deleteById(permissionId);
            r=!update? R.error("删除失败"):R.ok();
        }catch (Exception  e){
            r= R.error("ID编号不正确");
        }
        return r;
    }
    /**
     * 修改权限信息
     * @param request
     * @return
     */
    public R updateById(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader  bufferedReader=request.getReader();
        String  req_data=bufferedReader.readLine();
        Permissions permissions = JSONObject.parseObject(req_data, Permissions.class);
        PermissionService permissionService=new PermissionServiceImpl();

        boolean update = permissionService.update(permissions);
        R   r=!update? R.error("修改失败"):R.ok();
        return r;
    }
    /**
     * 查询所有菜单信息
     * @param request
     * @return
     */
    public R findAllPermissionList(HttpServletRequest request) {
        PermissionService permissionService=new PermissionServiceImpl();
        List<PermissionsOption> list= permissionService.findAllPermissionList();
        R r;
        r=list!=null?R.ok(list):R.ok("没有菜单数据");
        return r;
    }
    /**
     * 查询该角色菜单信息
     * @param request
     * @return
     */
    public R findCheckedPermissionList(HttpServletRequest request) {
        String id = request.getParameter("roleId");
        System.out.println(id);
        R  r;
        Integer roleId=null;
        try{
            roleId=Integer.parseInt(id);
            PermissionService permissionService=new PermissionServiceImpl();
            List<Integer> list= permissionService.findCheckedPermissionList(roleId);
            r=list!=null?R.ok(list):R.ok("该角色没有菜单数据");
        }catch (Exception  e){
            r= R.error("ID编号不正确");
        }
        return r;
    }
}
